package com.baifendian.controller;

import static java.util.concurrent.Executors.*;

import com.baifendian.common.task.APITask;
import com.baifendian.common.task.RcApiInterStatTask;
import com.baifendian.common.task.RcApiSysStatTask;
import com.baifendian.common.task.RcDataInterStatByDayTask;

import com.baifendian.common.task.RcDataInterStatByMonthTask;
import com.baifendian.common.task.RcDataSysStatExpTask;
import com.baifendian.common.task.RcDataSysStatImpTask;
import com.baifendian.common.utils.DateUtils;
import com.baifendian.service.CatalogService;
import com.baifendian.service.ExcApiTrackService;
import com.baifendian.service.RcApiInterStatService;
import com.baifendian.service.RcApiSysStatService;
import com.baifendian.service.RcDataInterStatService;
import com.baifendian.service.RcDataSysStatService;
import java.time.LocalDateTime;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author peng.guo on 2018/3/5.
 */
@RestController
public class MultiController {

  private ScheduledExecutorService ses = newScheduledThreadPool(2);

  @Autowired
  private CatalogService catalogService;

  @Autowired
  private ExcApiTrackService excApiTrackService;

  @Autowired
  private RcApiInterStatService rcApiInterStatService;

  @Autowired
  private RcApiSysStatService rcApiSysStatService;

  @Autowired
  private RcDataInterStatService rcDataInterStatService;

  @Autowired
  private RcDataSysStatService rcDataSysStatService;

  @GetMapping("/start")
  public void startThreadPool(){

//    ses.scheduleAtFixedRate(new SimulatedTask("scheduleAtFixedRate-1-"), 0, 2, TimeUnit.SECONDS);

    ses.scheduleAtFixedRate(new APITask(excApiTrackService,"APITask-2-"), 0, 1000, TimeUnit.SECONDS);
    ses.scheduleWithFixedDelay(new RcApiInterStatTask(rcApiInterStatService,"RcApiInterStatTask-2-"), 0, 1000, TimeUnit.SECONDS);
    ses.scheduleWithFixedDelay(new RcApiSysStatTask(rcApiSysStatService,"rcApiSysStatService-3-"), 0, 1000, TimeUnit.SECONDS);
//    ses.scheduleWithFixedDelay(new RcDataInterStatByDayTask(rcDataInterStatService,"rcDataInterStatService-3-"), 0, 1000, TimeUnit.SECONDS);
//    ses.scheduleWithFixedDelay(new RcDataInterStatByMonthTask(rcDataInterStatService,"rcDataInterStatService-3-"), 0, 1000, TimeUnit.SECONDS);
//    ses.scheduleWithFixedDelay(new RcDataSysStatImpTask(rcDataSysStatService,"rcDataSysStatService-5-"), 0, 1000, TimeUnit.SECONDS);
//    ses.scheduleWithFixedDelay(new RcDataSysStatExpTask(rcDataSysStatService,"rcDataSysStatService-5-"), 0, 1000, TimeUnit.SECONDS);

  }

  @GetMapping("/access")
  public JSONObject accessDatabase(){
    JSONObject json = new JSONObject();
    LocalDateTime currentTime = DateUtils.getCurrentTime();
    System.out.println("currentTime" + currentTime);
//    Category category = catalogService.findCategory(100);
//    json.accumulate(category.getName(), category);
    int totalApi = excApiTrackService.getTotalApi();
    System.out.println("totalApi : " + totalApi);
    json.accumulate("totalApi",totalApi);
    return json;
   }

}
